<?php


namespace app\service;


use app\service\exception\ApiException;
use app\service\model\AdminModel;
use app\service\model\MemberModel;

class Token
{

    /**
     * 用户获取token
     * @param $member_id
     * @return array
     * @throws ApiException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getMemberToken($member_id): array
    {
        $member = MemberModel::where('id', '=', $member_id)->find();
        if (!$member) {
            throw new ApiException(['msg' => '用户不存在']);
        }
        $token = 'member_token_' . md5(random_keys(18)) . $member_id;

        cache($token, ['member_id' => $member_id], 30 * 24 * 60 * 60, 'member_token');

        return compact('member', 'token');
    }

    /**
     * 验证用户token是否有效
     * @param $token
     * @return mixed|object|\think\App
     * @throws ApiException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function checkMemberToken() {
        $token = request()->header('token');
        if (strpos($token, 'member_token_') !== 0) {
            throw new ApiException(['msg' => '无效登录凭证，请重新登录', 'errorCode' => 1001]);
        }
        $data = cache($token);
        if (!$data || !isset($data['member_id'])) {
            cache($token, null);
            throw new ApiException(['msg' => '无效登录凭证，请重新登录', 'errorCode' => 1001]);
        }
        $member = MemberModel::where('id', '=', $data['member_id'])->find();
        if (!$member) {
            cache($token, null);
            throw new ApiException(['msg' => '无效登录凭证，请重新登录', 'errorCode' => 1001]);
        }
        // 验证通过刷新token时间
        cache($token, $data, 30 * 24 * 60 * 60, 'member_token');
        return $member;
    }

    /**
     * 管理员获取token
     * @param $admin_id
     * @return array
     * @throws ApiException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getAdminToken($admin_id): array
    {
        $admin = AdminModel::where('id', '=', $admin_id)->find();
        if (!$admin) {
            throw new ApiException(['msg' => '用户不存在']);
        }
        $token = 'admin_token_' . md5(random_keys(18)) . $admin_id;

        cache($token, ['admin_id' => $admin_id], 30 * 24 * 60 * 60, 'admin_login_token');

        return compact('admin', 'token');
    }

    /**
     * 验证用户token是否有效
     * @return array|\think\Model
     * @throws ApiException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function checkAdminToken() {
        $token = request()->header('token');
        if (strpos($token, 'admin_token_') !== 0) {
            throw new ApiException(['msg' => '无效登录凭证，请重新登录', 'errorCode' => 1001]);
        }
        $data = cache($token);
        if (!$data || !isset($data['admin_id'])) {
            cache($token, null);
            throw new ApiException(['msg' => '无效登录凭证，请重新登录', 'errorCode' => 1001]);
        }
        $admin = AdminModel::where('id', '=', $data['admin_id'])->find();
        if (!$admin) {
            cache($token, null);
            throw new ApiException(['msg' => '无效登录凭证，请重新登录', 'errorCode' => 1001]);
        }
        // 验证通过刷新token时间
        cache($token, $data, 30 * 24 * 60 * 60, 'admin_login_token');
        return $admin;
    }
}